package hermesV3.services.TupleSpace;

import java.io.Serializable;

import org.sercho.masp.space.EqualityChecker;
import org.sercho.masp.space.ReflectiveObjectMatcher;
import org.sercho.masp.space.SimpleObjectSpace;
import org.sercho.masp.space.TupleSpace;

/**
 * This class represent an interface to the TupleSpace
 * 
 * @author Ambruoso Luigi
 * @author Addeo Vincenzo
 */

public class Space implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7373379002494208408L;
	private static TupleSpace<Tuple> space;
	private static ReflectiveObjectMatcher refMatcher;

	/**
	 * This method use a design pattern similar to the Singleton
	 * 
	 * @return the istance of the TupleSpace
	 */
	public static TupleSpace<Tuple> getInstance() {
		if (space == null) {
			refMatcher = new ReflectiveObjectMatcher();

			refMatcher.addChecker(Tuple.class, new EqChecker());
			space = new SimpleObjectSpace<Tuple>(refMatcher, "Space");
		}

		return space;
	}

	/**
	 * This class compares the tuples, only for the research
	 * 
	 * @author Ambruoso Luigi
	 * @author Addeo Vincenzo
	 */
	private static class EqChecker implements EqualityChecker<Tuple> {

		private static final long serialVersionUID = 1L;

		public EqChecker() {
		}

		@Override
		public boolean equals(Tuple a, Tuple b) {

			return b.equalsSearch(a);
		}

	}
}
